package com.iv.durian.cloud.api;

import com.iv.durian.cloud.JsonResult;
import com.iv.durian.cloud.Notify;
import com.iv.durian.cloud.api.service.NotifyService;
import com.iv.durian.cloud.vo.NotifyVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/notify")
@Slf4j
@Api(value = "NotifyApi", description = "NotifyApi", tags = {"notify"})
public class NotifyApi {

    @Autowired
    NotifyService notifyService;

    @ApiOperation(value = "创建通知")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public void add(
            @ApiParam(name = "user_id", value = "用户id", required = true) @RequestParam(value = "user_id") int user_id,
            @ApiParam(name = "type", value = "通知类型 0 系统 1个人", required = true) @RequestParam(value = "type") int type,
            @ApiParam(name = "title", value = "通知标题", required = true) @RequestParam(value = "title") String title,
            @ApiParam(name = "content", value = "通知内容", required = true) @RequestParam(value = "content") String content,
            @ApiParam(name = "status", value = "已读状态 0 未读 1 已读", required = false) @RequestParam(value = "status", required = false, defaultValue = "0") Integer status
    ) {
        Notify notify = new Notify();
        notify.setTitle(title);
        notify.setType(type);
        notify.setContent(content);
        notify.setStatus(status);
        notify.setUserId(user_id);

        notifyService.add(notify);
    }

    @ApiOperation(value = "删除通知")
    @RequestMapping(value = "/remove/{id}", method = RequestMethod.POST)
    public void remove(
            @ApiParam(required = true, name = "id", value = "通知id") @PathVariable("id") int id
    ) {
        log.debug("删除的通知id:" + id);
        notifyService.removeById(id);
    }

    @ApiOperation(value = "修改通知")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public void update(@ApiParam(required = true, name = "notify", value = "修改信息") @RequestBody Notify notify) {
        notifyService.update(notify);
    }


    @ApiOperation(value = "获取通知详情")
    @RequestMapping(value = "/get/{id}", method = RequestMethod.POST)
    public ResponseEntity<JsonResult<Notify>> get(
            @ApiParam(required = true, name = "id", value = "通知id") @PathVariable("id") int id
    ) {
        JsonResult r = new JsonResult();

        try {
            Notify notify = notifyService.getById(id);
            if (notify != null) {
                NotifyVO notifyVO = NotifyVO.builder()
                        .id(notify.getId())
                        .title(notify.getTitle())
                        .content(notify.getContent())
                        .type(notify.getType())
                        .status(notify.getStatus())
                        .userId(notify.getUserId())
                        .createTime(notify.getCreateTime())
                        .updateTime(notify.getUpdateTime()).build();
                r.ok(notifyVO);
            }
        } catch (Exception e) {
            r.error(e.getClass().getName() + ":" + e.getMessage());
            log.error("通知信息查询失败", e);
        }
        return ResponseEntity.ok(r);
    }


    @ApiOperation(value = "通知列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ResponseEntity<JsonResult<List<NotifyVO>>> list(
            @ApiParam(required = false, name = "start", value = "分页起始值") @RequestParam(value = "start", required = false) Integer start,
            @ApiParam(required = false, name = "max", value = "每页记录数") @RequestParam(value = "max", required = false) Integer max,
            @ApiParam(required = false, name = "user_id", value = "用户id") @RequestParam(value = "user_id", required = false) Integer user_id,
            @ApiParam(required = false, name = "type", value = "通知类型 0 系统 1个人") @RequestParam(value = "type", required = false) Integer type,
            @ApiParam(required = false, name = "status", value = "已读状态 0 未读 1 已读") @RequestParam(value = "status", required = false) Integer status

    ) {
        JsonResult r = new JsonResult();
        try {
            List<Notify> notifyList = notifyService.getlist(user_id, status, type, start, max);
            List<NotifyVO> notifyVOList =  notifyList.stream().map(notify -> {
                NotifyVO notifyVO = NotifyVO.builder()
                        .id(notify.getId())
                        .title(notify.getTitle())
                        .content(notify.getContent())
                        .type(notify.getType())
                        .status(notify.getStatus())
                        .userId(notify.getUserId())
                        .createTime(notify.getCreateTime())
                        .updateTime(notify.getUpdateTime()).build();
                return notifyVO;
            }).collect(Collectors.toList());

            r.ok(notifyVOList);
        } catch (Exception e) {
            r.error(e.getClass().getName() + ":" + e.getMessage());
            log.error("通知列表列表信息查询失败", e);
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "获取通知数量")
    @RequestMapping(value = "/count", method = RequestMethod.POST)
    public ResponseEntity<JsonResult<Integer>> count(
            @ApiParam(required = false, name = "user_id", value = "用户id") @RequestParam(value = "user_id", required = false) Integer user_id,
            @ApiParam(required = false, name = "type", value = "通知类型 0 系统 1个人") @RequestParam(value = "type", required = false) Integer type,
            @ApiParam(required = false, name = "status", value = "已读状态 0 未读 1 已读") @RequestParam(value = "status", required = false) Integer status
    ) {
        JsonResult r = new JsonResult();
        Notify notify = new Notify();
        notify.setUserId(user_id);
        notify.setType(type);
        notify.setStatus(status);
        try {
            r.ok(notifyService.count(notify));
        } catch (Exception e) {
            r.error(e.getClass().getName() + ":" + e.getMessage());
            log.error("通知信息查询失败", e);
        }
        return ResponseEntity.ok(r);
    }

}
